# -*- coding: utf_8 -*-
from sqlite3.test import transactions

import config
from logs import  logparser, apachelogs
import time
import getopt
import sys

def analyse_clickstream(paths):

    if config.remove_robot:
        log = apachelogs.ApacheLogFile(*paths)
        parser = logparser.LogParser(log)
        parser.remove_robot()
    if config.clean_log:
        log = apachelogs.ApacheLogFile(config.output_path + "no_robot_log")
        parser = logparser.LogParser(log)
        parser.clean_log()
    if config.generate_sql_scr:
        log = apachelogs.ApacheLogFile(config.output_path + "purged_log")
        parser = logparser.LogParser(log)        
        parser.generate_sql_scr()
   
if __name__ == "__main__":
    try:                                
        opts, args = getopt.getopt(sys.argv[1:], "a:o:w", ["action=","outpath=","with_query_string"])
    except getopt.GetoptError:
        sys.exit(2)
        
    for opt, arg in opts:
        if opt in ("-a", "--action"):  
            if arg.find("r") >= 0:
                config.remove_robot = True
            if arg.find("g") >= 0:
                config.generate_sql_scr = True
            if arg.find("c") >= 0:
                config.clean_log = True
        elif opt in ("-o", "--outpath"):
            config.output_path = arg
        elif opt in ("-w", "--with_query_string"):
            config.remove_query_string = False

    analyse_clickstream(config.filename)